package com.zhaosc.elasticsearch;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.client.core.TermVectorsResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.junit.Test;

/**
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-document-term-vectors.html
 * 
 * @author zhaosc
 *
 */
public class TermVectorsTest extends BaseTest {

	private static final Logger logger = Logger.getLogger(TermVectorsTest.class);

	@Test
	public void testTermVectors01() {
		TermVectorsRequest request = new TermVectorsRequest(movies, "3843");
		request.setFields("title");
		
		try {
			TermVectorsResponse response = client.termvectors(request, RequestOptions.DEFAULT);
			for (TermVectorsResponse.TermVector tv : response.getTermVectorsList()) {
			    String fieldname = tv.getFieldName(); 
			    int docCount = tv.getFieldStatistics().getDocCount(); 
			    long sumTotalTermFreq =
			            tv.getFieldStatistics().getSumTotalTermFreq(); 
			    long sumDocFreq = tv.getFieldStatistics().getSumDocFreq(); 
			    if (tv.getTerms() != null) {
			        List<TermVectorsResponse.TermVector.Term> terms =
			                tv.getTerms(); 
			        for (TermVectorsResponse.TermVector.Term term : terms) {
			            String termStr = term.getTerm(); 
			            int termFreq = term.getTermFreq(); 
			            int docFreq = term.getDocFreq(); 
			            long totalTermFreq = term.getTotalTermFreq(); 
			            float score = term.getScore(); 
			            if (term.getTokens() != null) {
			                List<TermVectorsResponse.TermVector.Token> tokens =
			                        term.getTokens(); 
			                for (TermVectorsResponse.TermVector.Token token : tokens) {
			                    int position = token.getPosition(); 
			                    int startOffset = token.getStartOffset(); 
			                    int endOffset = token.getEndOffset(); 
			                    String payload = token.getPayload(); 
			                }
			            }
			        }
			    }
			}
		} catch (IOException e) {
			logger.error(e);
		}
		
	}

	@Test
	public void testTermVectors02() {
		try {
			XContentBuilder docBuilder = XContentFactory.jsonBuilder();
			docBuilder.startObject().field("title", "Sleepaway Camp").endObject();
			TermVectorsRequest request = new TermVectorsRequest(movies, docBuilder);
			try {
				TermVectorsResponse response = client.termvectors(request, RequestOptions.DEFAULT);
				for (TermVectorsResponse.TermVector tv : response.getTermVectorsList()) {
				    String fieldname = tv.getFieldName(); 
				    int docCount = tv.getFieldStatistics().getDocCount(); 
				    long sumTotalTermFreq =
				            tv.getFieldStatistics().getSumTotalTermFreq(); 
				    long sumDocFreq = tv.getFieldStatistics().getSumDocFreq(); 
				    if (tv.getTerms() != null) {
				        List<TermVectorsResponse.TermVector.Term> terms =
				                tv.getTerms(); 
				        for (TermVectorsResponse.TermVector.Term term : terms) {
				            String termStr = term.getTerm(); 
				            int termFreq = term.getTermFreq(); 
				            int docFreq = term.getDocFreq(); 
				            long totalTermFreq = term.getTotalTermFreq(); 
				            float score = term.getScore(); 
				            if (term.getTokens() != null) {
				                List<TermVectorsResponse.TermVector.Token> tokens =
				                        term.getTokens(); 
				                for (TermVectorsResponse.TermVector.Token token : tokens) {
				                    int position = token.getPosition(); 
				                    int startOffset = token.getStartOffset(); 
				                    int endOffset = token.getEndOffset(); 
				                    String payload = token.getPayload(); 
				                }
				            }
				        }
				    }
				}
			} catch (IOException e) {
				logger.error(e);
			}
		} catch (IOException e) {
			logger.error(e);
		}

	}

}
